<?php 

	//Connect to database
	include '../dbc.php';

	
    $telephone	= $_POST['telephone'];
 	$passcode 	= $_POST['passcode'];
	$imei		= $_POST['imei'];
	

	//Search for user in database
	//Check is activated
    $query01 = "SELECT * FROM user WHERE telephone = '$telephone'";
	$query01_result = mysql_query($query01);
	$query01_result_row = mysql_fetch_array($query01_result);
	$uid = $query01_result_row['id'];
	
	$date = $register_date = date("Y-m-d H:i:s");
	$uid = $query01_result_row['id'];
	$sql_verify = "SELECT * FROM user_verify WHERE user='$uid' AND passcode='$passcode'";
	
	$sql_verify_result = mysql_query($sql_verify);
	
	//Old version of our app does not update imei here. Force them to update application
	if(mysql_num_rows($sql_verify_result)>0 && isset($_POST['imei'])){
		//Update information in the user table
		$last_login		= date("Y-m-d H:i:s");
		$query_update_user = "UPDATE user SET last_login='$last_login',is_activated='1',imei='$imei' WHERE telephone='$telephone'";
		$query_update_user_result = mysql_query($query_update_user);
		
		// Allow login to app
		$response["MESSAGE"] = "CORRECT";
		echo json_encode($response);
	}else{
		// Forbid login to app
		$response["MESSAGE"] = "WRONG";
		$response["UPDATE"] = "YES";
		echo json_encode($response);
	}

?>
